// pages/share/share.js
var app = getApp(); //获取应用实例
const quote = app.globalData.quote;
const mta = quote.mta;
const api = quote.api;
const tools = quote.tools;
const path = quote.paths;


Page({

    /**
     * 页面的初始数据
     */
    data: {
        QID: null,
        showLoading: false,
        saveSuccess: false,
        QRcode: null,
        score: 80,
        windowWidth: 375,
        windowHeight: 667,
        scale: 1,
        loadingHidden: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        console.log('加载分享页获取到的数据', options)

        mta.Page.init();

        that.setData({
            QID: options.QID,
            score: options.score,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        var that = this;
        // 生命周期函数--监听页面初次渲染完成

        var ctx = wx.createCanvasContext('mycanvas');
        var score = that.data.score || 80;
        var text1 = '我在口语秀配音中拿到' + score + '分，';
        var text2 = '敢与我来一场殿堂级的较量吗？';

        wx.getSystemInfo({
            success: function(res) {

                var width = parseInt(res.windowWidth)
                var height = parseInt(res.windowHeight)
                var scale = parseFloat(res.windowWidth) / 375.0

                that.setData({
                    windowWidth: width,
                    windowHeight: height,
                    scale: scale,
                })

                //绘制白色背景
                ctx.setFillStyle('white');
                ctx.fillRect(0, 0, 375 * scale, 550 * scale);
                ctx.draw(true);

                //绘制顶部文字
                ctx.drawImage(path.images + 'semicolon_front.png', 25 * scale, 20 * scale, 24.5 * scale, 20.5 * scale);
                ctx.draw(true);
                ctx.setFontSize(20 * scale);
                ctx.setFillStyle('#16ccbd');
                ctx.fillText(text1, 55 * scale, 45 * scale);
                ctx.fillText(text2, 45 * scale, 80 * scale);
                ctx.drawImage(path.images + 'semicolon_after.png', 330 * scale, 70 * scale, 24.5 * scale, 20.5 * scale);
                ctx.draw(true);

                //绘制中间插图
                ctx.drawImage(path.images + 'posterimg.png', 55 * scale, 100 * scale, 270 * scale, 295 * scale);
                ctx.draw(true);

                //绘制小程序码
                tools.getWxCode({
                    scene: app.globalData.openId + '&' + that.data.QID,
                    page: 'pages/score/score'
                }, function(res) {
                    console.log('后台获取的小程序码', res.data.data);
                    wx.downloadFile({
                        url: res.data.data,
                        success: function(res) {
                            console.log('下载二维码成功', res);
                            if (res.statusCode == 200) {
                                ctx.drawImage(res.tempFilePath, 60 * scale, 400 * scale, 100 * scale, 100 * scale);
                                ctx.draw(true);
                                that.setData({
                                    loadingHidden: true
                                })
                            } else {
                                console.log('获取二维码失败');
                                ctx.drawImage(path.images + 'qrcode.jpg', 60 * scale, 400 * scale, 100 * scale, 100 * scale);
                                ctx.draw(true);
                                that.setData({
                                    loadingHidden: true
                                })
                            }
                        },
                        fail: function(err) {
                            console.log('下载二维码失败', err);
                            ctx.drawImage(path.images + 'qrcode.jpg', 60 * scale, 400 * scale, 100 * scale, 100 * scale);
                            ctx.draw(true);
                            that.setData({
                                loadingHidden: true
                            })
                        }
                    })
                }, function(err) {
                    ctx.drawImage(path.images + 'qrcode.jpg', 60 * scale, 400 * scale, 100 * scale, 100 * scale);
                    ctx.draw(true);
                    that.setData({
                        loadingHidden: true
                    })
                })

                //绘制文字
                ctx.setFontSize(18 * scale);
                ctx.setFillStyle('black')
                ctx.fillText('长  按  识  别', 200 * scale, 440 * scale);
                ctx.draw(true)
                ctx.setFontSize(27 * scale);
                ctx.setFillStyle('#16ccbd')
                ctx.fillText('练习口语', 195 * scale, 480 * scale);
                ctx.draw(true)
            },
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    saveToAlbum: function() {
        var that = this;

        that.setData({
            showLoading: true,
        })

        app.aldstat.sendEvent('得分-点击保存至朋友圈');
        mta.Event.stat("score_savefriendscircle", {});

        if (that.data.saveSuccess) {
            wx.navigateBack({
                delta: 1
            })
        } else {

            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 375 * that.data.scale,
                height: 550 * that.data.scale,
                destWidth: 375 * that.data.scale * 5,
                destHeight: 550 * that.data.scale * 5,
                fileType: 'jpg',
                canvasId: 'mycanvas',
                quality: 1,
                success: function(res) {
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success(res) {
                            console.log('保存文件成功', res);
                            app.aldstat.sendEvent('得分-保存至朋友圈成功');
                            mta.Event.stat("score_savefriendscirclesuccess", {});
                            wx.showToast({
                                title: '海报保存成功',
                                icon: 'success',
                                duration: 2000
                            })
                            that.setData({
                                showLoading: false,
                                saveSuccess: true,
                            })
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 1, // 回退前 delta(默认为1) 页面
                                })
                            }, 2000);
                        }
                    })
                }
            })
        }
    }
})